/*
 * Packet.h
 *
 *  Created on: Apr 14, 2011
 *      Author: Alan Mattila
 *
 *  The Packet class defines the structure of the packet
 *  that will be sent over the network.
 */

#ifndef PACKET_H_
#define PACKET_H_

#include <cstdint>

#include "../board/Board.h"

#define MAX_PACKET_SIZE 66
#define BOARD_SIZE (BOARD_X*BOARD_Y)
#define MESSAGE_PACKET_SIZE 2
#define GAME_DATA_PACKET_SIZE 66
#define MOVE_PACKET_SIZE 5

enum packetType_t { Message, GameData, Move };
enum packetMessage_t { IllegalMove };

class Packet
{
public:
	Packet();
	~Packet();

	// Accessors
	packetType_t GetPacketType(void) const;
	packetMessage_t GetPacketMessage(void) const;
	void GetPacketGameBoard(Board *board) const;
	int GetPacketStatus(void) const;
	int GetPacketX1(void) const;
	int GetPacketY1(void) const;
	int GetPacketX2(void) const;
	int GetPacketY2(void) const;

	// Mutators
	void SetPacketType(packetType_t packetType);
	void SetPacketMessage(packetMessage_t packetMessage);
	void SetPacketGameBoard(Board *board);
	void SetPacketStatus(int packetStatus);
	void SetPacketMove(int x1, int y1, int x2, int y2);

	int GetPacketSize(void) const;
	uint8_t GetByte(int index) const;
	void SetByte(uint8_t byte, int index);
	void DisplayContents(void);

private:

// Align on byte boundaries
#pragma pack(1)
	union
	{
		uint8_t data[MAX_PACKET_SIZE];
		struct
		{
			uint8_t type;
			union
			{
				uint8_t message;
				struct
				{
					uint8_t gameBoard[BOARD_SIZE];
					uint8_t status;
				};
				struct
				{
					uint8_t x1;
					uint8_t y1;
					uint8_t x2;
					uint8_t y2;
				};
			};
		} packetData;
	};
};


#endif /* PACKET_H_ */
